﻿Option Explicit On
Imports Llama
Imports System.Data.SqlClient
Public Class DrawingNumberBatchProp

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim phase As String = ""
        Dim Code As String = ""
        'If Me.DesignPhase.Text = "基础设计(B)" Then
        '    phase = "B"
        'ElseIf Me.DesignPhase.Text = "详细设计(D)" Then
        '    phase = "D"
        'ElseIf Me.DesignPhase.Text = "工艺包设计(P)" Then
        '    phase = "P"
        If InStr(Me.DesignPhase.Text, "(") > 0 Then
            phase = Mid(Me.DesignPhase.Text, InStr(Me.DesignPhase.Text, "(") + 1, 1)
        End If
        'End If

        If Discipline.Text = "工艺(PS)" Then
            Code = "PS"
        ElseIf Discipline.Text = "给排水(WS)" Then
            Code = "WS"
        ElseIf Discipline.Text = "暖通空调(HV)" Then
            Code = "HV"
            '工艺(PS)
            '给排水(WS)
            '暖通空调(HV)
        End If
        DrawingNumberBatchUpdate(phase, Code, Me.DW004.Text, "Selected", "否", Me.CheckBox1.Checked, Me.CheckBox2.Checked)
    End Sub


    Public Sub DrawingNumberBatchUpdate(Phase As String, Discipline As String, DWxxx As String, Mode As String, IfSPF As String, bPage As Boolean, bTotalPage As Boolean)    '图号、版次批量，Page排序
        '如果不打开图纸的话，Title Block那，在打印时还是呈现老的DrawingNumber
        Dim objDrawings As LMDrawings = New LMDrawings
        Dim objUnit As LMUnit
        Dim objPlantGroups As LMPlantGroups
        Dim objFilter As LMAFilter
        'objDrawings.Collect(DSInDrawingProp)
        ' DSInDrawingProp.BeginTransaction()
        Dim COUNT As Integer = 0
        Dim strDrawingNameLast As String = ""
        'MainForm.ProgressBar1.Maximum = objDrawings.Count
        'MainForm.ProgressBar1.Minimum = 0
        'RadForm1.RadProgressBarElement1.Minimum = 0
        Dim LastLowestPlantGroupID As String = ""
        Dim TotalPage As Integer = 0
        'Dim DrawingList As DockLeft = FormList.Item(0)   '靠arraylist作为中转，否则读不到mainform下的form
        Dim objDrawingsPerGroup As LMDrawings
        'objPlantGroups = New LMPlantGroups
        'objPlantGroups.Collect(DSInDrawingProp)
        Dim PageSort As ArrayList

        If Mode = "All" Then
            objDrawings.Collect(DS)
            DS.BeginTransaction()
            'RadForm1.RadProgressBarElement1.Maximum = objDrawings.Count
            objPlantGroups = New LMPlantGroups
            objPlantGroups.Collect(DS)

            For Each objPlantGroupInLoop As LMPlantGroup In objPlantGroups    '所有单元

                objFilter = New LMAFilter
                objDrawings = New LMDrawings
                objFilter.ItemType = "Drawing"
                objFilter.Criteria.AddNew("1")
                objFilter.Criteria.Item("1").SourceAttributeName = "SP_PlantGroupID"
                objFilter.Criteria.Item("1").ValueAttribute = objPlantGroupInLoop.Id        '   Unit的id
                objFilter.Criteria.Item("1").Operator = "="
                objDrawings.Collect(DS, Filter:=objFilter)
                TotalPage = objDrawings.Count

                PageSort = New ArrayList

                For Each objDrawingInLoop As LMDrawing In objDrawings    '单元下的所有图纸
                    objUnit = DS.GetUnit(objDrawingInLoop.PlantGroupID)

                    Try
                        strDrawingNameLast = "0" + Mid(objDrawingInLoop.Attributes("Name").Value, Len(objDrawingInLoop.Attributes("Name").Value) + 1 - 3) '取最后3位

                        If IsNumeric(strDrawingNameLast) Then
                        Else
                            strDrawingNameLast = "00" + Mid(strDrawingNameLast, 3)  '取最后2位,共4位,0xxx
                        End If
                        PageSort.Add(strDrawingNameLast + "|" + objDrawingInLoop.Id)

                        'Title Block Revision from Revision 只是要第一位

                        If objDrawingInLoop.Revisions.Count > 0 Then
                            '更新title block revision属性
                            objDrawingInLoop.Attributes("Revision").Value = objDrawingInLoop.Revisions.Nth(objDrawingInLoop.Revisions.Count).Attributes("SP_MajorRevisionNumber").Value
                            objDrawingInLoop.Commit()
                            '+                        objDrawingInLoop.Revisions.Nth(objDrawingInLoop.Revisions.Count).Attributes("SP_MinorRevisionNumber").Value



                            'spf版次有的话
                            'Drawing Number
                            objDrawingInLoop.Attributes("DrawingNumber").Value = String.Format("{0}-{1}-{2}-{3}-{4}-{5}{6}",
                                                                                       DS.ProjectNumber,
                                                                                       Phase,
                                                                                       objUnit.Attributes("Name").Value,
                                                                                       Discipline,
                                                                                       DWxxx,
                                                                                       strDrawingNameLast,
                                                                                       objDrawingInLoop.Revisions.Nth(objDrawingInLoop.Revisions.Count).Attributes("SP_MajorRevisionNumber").Value)   'spf的D01的版次，也是只取D
                            objDrawingInLoop.Commit()
                        Else

                            'spf的版次没有的话
                            'Drawing Number
                            objDrawingInLoop.Attributes("DrawingNumber").Value = String.Format("{0}-{1}-{2}-{3}-{4}-{5}{6}",
                                                                                       DS.ProjectNumber,
                                                                                       Phase,
                                                                                       objUnit.Attributes("Name").Value,
                                                                                       Discipline,
                                                                                       DWxxx,
                                                                                       strDrawingNameLast,
                                                                                        objDrawingInLoop.Attributes("Revision").Value)
                            objDrawingInLoop.Commit()
                        End If



                        '版次1
                        Try
                            objDrawingInLoop.Attributes("RevisedVersion1").Value = objDrawingInLoop.Revisions.Nth(objDrawingInLoop.Revisions.Count).Attributes("SP_MajorRevisionNumber").Value + objDrawingInLoop.Revisions.Nth(objDrawingInLoop.Revisions.Count).Attributes("SP_MinorRevisionNumber").Value
                            objDrawingInLoop.Commit()
                        Catch

                        End Try


                        'Page and TotalPage
                        'Try
                        '    objDrawingInLoop.Attributes("TotalPage").Value = TotalPage

                        'Catch

                        'End Try
                        objFilter = Nothing
                        objFilter = New LMAFilter

                        objDrawingInLoop.Commit()
                    Catch ex As Exception
                        MessageBox.Show("Drawing (" + objDrawingInLoop.Attributes("Name").Value + ") is not supported. Skip it.")
                    End Try
                    COUNT = COUNT + 1

                    'MainForm.ProgressBar1.Value = COUNT
                    'RadForm1.RadProgressBarElement1.Value1 = COUNT
                    Try
                        UpdateGlobalDrawing(objDrawingInLoop.Attributes("DrawingNumber").Value, objDrawingInLoop.Id, "DrawingNumber")              '同样,需要在drawingNumber更新后,去处理GlobalDrawing的表格
                        UpdateDrawingSite(objDrawingInLoop.Attributes("DrawingNumber").Value, objDrawingInLoop.Id, "DrawingNumber")              '同样,需要在drawingNumber更新后,去处理drawingsite的表格
                        UpdateDrawingVersion(objDrawingInLoop.Attributes("DrawingNumber").Value, objDrawingInLoop.Id, "DrawingNumber")      '默认情况下,通过API,不会更新drawingsite中的数据
                    Catch
                    End Try
                Next
                'PageSort.Sort()

                'Try
                '    For Each objDrawingInLoop As LMDrawing In objDrawings
                '        'Page
                '        strDrawingNameLast = "0" + Mid(objDrawingInLoop.Attributes("Name").Value, Len(objDrawingInLoop.Attributes("Name").Value) + 1 - 3) '取最后3位

                '        If IsNumeric(strDrawingNameLast) Then
                '        Else
                '            strDrawingNameLast = "00" + Mid(strDrawingNameLast, 3)  '取最后2位,共4位,0xxx
                '        End If

                '        objDrawingInLoop.Attributes("Page").Value = 1 + PageSort.IndexOf(strDrawingNameLast + "|" + objDrawingInLoop.Id)
                '        objDrawingInLoop.Commit()
                '    Next
                'Catch

                'End Try
                objFilter = Nothing
                objDrawings = Nothing

            Next
            DS.CommitTransaction()
        ElseIf Mode = "Selected" Then    ' All的模式估计都不会去用，都是根据选择的图纸来
            For i = 0 To DrawingCol.Count - 1
                objDrawings.Add(DS.GetDrawing(DrawingCol.Item(i).ToString))
            Next
            PageSort = New ArrayList
            DS.BeginTransaction()
            TotalPage = objDrawings.Count
            'MainForm.ProgressBar1.Maximum = objDrawings.Count
            'RadForm1.RadProgressBarElement1.Maximum = objDrawings.Count
            For Each objDrawingInLoop As LMDrawing In objDrawings    'treeview中选中的图纸
                objUnit = DS.GetUnit(objDrawingInLoop.PlantGroupID)

                Try
                    strDrawingNameLast = "0" + Mid(objDrawingInLoop.Attributes("Name").Value, Len(objDrawingInLoop.Attributes("Name").Value) + 1 - 3) '取最后3位

                    If IsNumeric(strDrawingNameLast) Then
                    Else
                        strDrawingNameLast = "00" + Mid(strDrawingNameLast, 3)  '取最后2位,共4位,0xxx
                    End If
                    PageSort.Add(strDrawingNameLast + "|" + objDrawingInLoop.Id)

                    'Title Block Revision from Revision 只是要第一位
                    If IfSPF = "是" And objDrawingInLoop.Revisions.Count > 0 Then
                        'Title Blcok Revision属性
                        objDrawingInLoop.Attributes("Revision").Value = objDrawingInLoop.Revisions.Nth(objDrawingInLoop.Revisions.Count).Attributes("SP_MajorRevisionNumber").Value
                        objDrawingInLoop.Commit()
                        'Drawing Number属性
                        objDrawingInLoop.Attributes("DrawingNumber").Value = String.Format("{0}-{1}-{2}-{3}-{4}-{5}{6}",
                                                                                   DS.ProjectNumber,
                                                                                   Phase,
                                                                                   objUnit.Attributes("Name").Value,
                                                                                   Discipline,
                                                                                   DWxxx,
                                                                                   strDrawingNameLast,
                                                                                   objDrawingInLoop.Revisions.Nth(objDrawingInLoop.Revisions.Count).Attributes("SP_MajorRevisionNumber").Value)   'spf的D01的版次，也是只取D
                        objDrawingInLoop.Commit()
                    Else   '包括  否，以及   是，但是并没有spf版次时，  这2种情况
                        'Title Blcok Revision属性
                        'Revision 就是以Title Blcok Revision为准了
                        'Drawing Number属性
                        objDrawingInLoop.Attributes("DrawingNumber").Value = String.Format("{0}-{1}-{2}-{3}-{4}-{5}{6}",
                                                                                   DS.ProjectNumber,
                                                                                   Phase,
                                                                                   objUnit.Attributes("Name").Value,
                                                                                   Discipline,
                                                                                   DWxxx,
                                                                                   strDrawingNameLast,
                                                                                    objDrawingInLoop.Attributes("Revision").Value)
                        objDrawingInLoop.Commit()
                    End If

                    '版次1,但由于其为select list,所有有可能拿过来后,依然会是空值 
                    'Try
                    '不需要处理
                    '    objDrawingInLoop.Attributes("RevisedVersion1").Value = objDrawingInLoop.Revisions.Nth(objDrawingInLoop.Revisions.Count).Attributes("SP_MajorRevisionNumber").Value + objDrawingInLoop.Revisions.Nth(objDrawingInLoop.Revisions.Count).Attributes("SP_MinorRevisionNumber").Value
                    'Catch

                    'End Try


                    'Page and TotalPage
                    Try
                        If bTotalPage Then
                            objDrawingInLoop.Attributes("TotalPage").Value = TotalPage   '选中的图纸总张数 
                            objDrawingInLoop.Commit()
                        End If
                    Catch

                    End Try

                    objDrawingInLoop.Commit()
                Catch ex As Exception
                    MessageBox.Show("Drawing (" + objDrawingInLoop.Attributes("Name").Value + ") 该图纸的Name太短。将跳过该图纸。")
                End Try
                COUNT = COUNT + 1

                'MainForm.ProgressBar1.Value = COUNT
                'RadForm1.RadProgressBarElement1.Value1 = COUNT
                Try
                    UpdateGlobalDrawing(objDrawingInLoop.Attributes("DrawingNumber").Value, objDrawingInLoop.Id, "DrawingNumber")
                    UpdateDrawingSite(objDrawingInLoop.Attributes("DrawingNumber").Value, objDrawingInLoop.Id, "DrawingNumber")   '避免放置opc时，看到的图纸名称不对
                    UpdateDrawingVersion(objDrawingInLoop.Attributes("DrawingNumber").Value, objDrawingInLoop.Id, "DrawingNumber")   '避免放置opc时，看到的图纸名称不对
                Catch
                End Try

            Next

            PageSort.Sort()

            Try
                If bPage Then
                    For Each objDrawingInLoop As LMDrawing In objDrawings
                        'Page
                        strDrawingNameLast = "0" + Mid(objDrawingInLoop.Attributes("Name").Value, Len(objDrawingInLoop.Attributes("Name").Value) + 1 - 3) '取最后3位

                        If IsNumeric(strDrawingNameLast) Then
                        Else
                            strDrawingNameLast = "00" + Mid(strDrawingNameLast, 3)  '取最后2位,共4位,0xxx
                        End If

                        objDrawingInLoop.Attributes("Page").Value = 1 + PageSort.IndexOf(strDrawingNameLast + "|" + objDrawingInLoop.Id)
                        objDrawingInLoop.Commit()
                    Next
                End If

            Catch

            End Try
            Try
                DS.CommitTransaction()
            Catch
            End Try
        End If

        'DSInDrawingProp.CommitTransaction()
        Me.Hide()
        'RadForm1.RadProgressBarElement1.Value1 = 0
        'DrawingList = Nothing
        objDrawings = Nothing
        objDrawingsPerGroup = Nothing
        COUNT = Nothing
        strDrawingNameLast = Nothing
        objPlantGroups = Nothing
        PageSort = Nothing

        MessageBox.Show("点击 ""刷新"" 以查看结果")
    End Sub
    Private Sub DrawingNumberBatchProp_Load(sender As Object, e As EventArgs) Handles MyBase.Load


        Me.PlantName.Text = DS.ProjectNumber
        Me.DesignPhase.Text = Me.DesignPhase.Items(0)
        Me.UnitName.Text = "Unit Name"
        Me.Discipline.Text = Me.Discipline.Items(0)
        Me.DrawingName.Text = "DrawingName & Revision"

        Me.IfSPF.Text = "是"

    End Sub

    Private Sub DesignPhase_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DesignPhase.SelectedIndexChanged
        Me.DesignPhase.Text = Me.DesignPhase.SelectedItem
    End Sub

    Private Sub Discipline_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Discipline.SelectedIndexChanged
        Me.Discipline.Text = Me.Discipline.SelectedItem
        If Discipline.Text = "工艺(PS)" Then
            DW004.Text = "DW004"
        ElseIf Discipline.Text = "给排水(WS)" Then
            DW004.Text = "DW002"
        ElseIf Discipline.Text = "暖通空调(HV)" Then
            DW004.Text = "DW00X"
        End If
    End Sub


    Private Sub IfSPF_SelectedIndexChanged(sender As Object, e As EventArgs) Handles IfSPF.SelectedIndexChanged
        Me.IfSPF.Text = Me.IfSPF.SelectedItem
    End Sub
End Class